import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:shop/models/categoryModel.dart';
import 'package:shop/models/focusModel.dart';
import 'package:shop/models/plistModel.dart';
import 'package:shop/tools/httpsClient.dart';

class HomeController extends GetxController {
  //TODO: Implement HomeController
  ScrollController scrollController = ScrollController();
  RxBool flag = false.obs;
  RxList<FocusItemModel> swiperList = <FocusItemModel>[].obs; //定义成响应式
  // 热销甄选数据
  RxList<FocusItemModel> bestSellingList = <FocusItemModel>[].obs; //定义成响应式
  RxList<CategoryItemModel> categoryList = <CategoryItemModel>[].obs; //定义成响应式
  // 热销臻选右侧
  RxList<plistItemModel> pList = <plistItemModel>[].obs; //定义成响应式
  // 瀑布流的数据
  RxList<plistItemModel> waterFlowList = <plistItemModel>[].obs; //定义成响应式

  // httpsClient
  HttpsClient httpsClient = HttpsClient();

  @override
  void onInit() {
    super.onInit();
    scrollerControllerListener();
    getFocusData();
    //   获取分类数据
    getCategoryData();
    //   热销甄选轮播图
    getBestSellingData();
    //   热销甄选右侧数据
    getpListData();
    //   瀑布流best数据
    getWaterFlowData();
  }

  void scrollerControllerListener() {
    scrollController.addListener(() {
      if (scrollController.position.pixels > 10) {
        if (flag == false) {
          flag.value = true;
          update();
        }
      }
      if (scrollController.position.pixels < 10) {
        if (flag == true) {
          flag.value = false;
          update();
        }
      }
    });
  }

  getFocusData() async {
    var response = await httpsClient.get('/api/focus');
    // print(response.data['result']);
    // swiperList.value = response.data['result'];
    if (response != null) {
      var focus = FocusModel.fromJson(response.data);
      swiperList.value = focus.result!;
      update();
    }
  }

  // 热销甄选轮播图
  getBestSellingData() async {
    var response = await httpsClient.get('/api/focus?position=2');
    if (response != null) {
      var focus = FocusModel.fromJson(response.data);
      bestSellingList.value = focus.result!;
      update();
    }
  }

  //   获取首页category数据
  getCategoryData() async {
    var response = await httpsClient.get('/api/bestCate');

    if (response != null) {
      var category = CategoryModel.fromJson(response.data);
      categoryList.value = category.result!;
      update();
    }
  }

  //   获取热销臻选右侧数据
  getpListData() async {
    var response = await httpsClient.get('/api/plist?is_hot=1');
    if (response != null) {
      var bestList = plistModel.fromJson(response.data);
      pList.value = bestList.result!;
      print('解析后全部数据：$pList');
      update();
    }
  }

  //   获取瀑布流数据
  getWaterFlowData() async {
    var response = await httpsClient.get('/api/plist?is_best=1');
    if (response != null) {
      var bestList = plistModel.fromJson(response.data);
      waterFlowList.value = bestList.result!;
      print('解析后全部数据22：$waterFlowList');
      update();
    }
  }
}
